/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//Variables
var userchatid      = new Array();
var chatoutput      = new Array();

Object.prototype.$ = function(id) {
    return document.getElementById(id);
}

/*
Array.prototype.contains = function ( needle ) {
   for (i in this) {
       if (this[i] == needle) return true;
   }
   return false;
}
*/

function checkInputUser(nama) {

    var inputchat   = $('chattext').value;
    if(inputchat.charAt(0) == '/') {
        checkCommandUser(inputchat);
    } else {
        $('outchat').innerHTML += "(" + nama + ") " + $('chattext').value + "<br>";
    }
    
    $('chattext').value ="";
    $('centerin').scrollTop  = $('outchat').offsetHeight;
}

function checkInputUserOwner () {
    var inputchat   = $('chattext').value;
    if(inputchat.chatAt(0) == '/') {
        checkCommandUser(inputchat);
        checkCommandOwner(inputchat);
    } else {

    }
    $('chattext').value =" ";
}

function checkCommandUser(inputchat) {
    //if command input
    arrayCommand   = inputchat.split(" ");
    switch (arrayCommand[0])  {
        case "/create":
            createRoom(arrayCommand);
            break;
        case "/join":
            joinRoom(arrayCommand);
            break;
        case "/chat":
            chatUser(arrayCommand);
            break;
        case "/addfriend":
            addFriend(arrayCommand);
            break;
        case "/exit":
            exit();
            break;
        case "/info":
            info(arrayCommand);
            break;
        default:
            alert("not a command");
            break;
    }

}

function checkCommandOwner(inputchat) {
    //if command input
    arrayCommand   = inputchat.split(" ");
    switch (arrayCommand[0])  {
        case "/kick":
            kick(arrayCommand);
            break;
        case "/ban":
            ban(arrayCommand);
            break;
        case "/unban":
            unband(arrayCommand);
            break;
        case "/persist":
            persist(arrayCommand);
            break;
        case "/setowner":
            setOwner(arrayCommand);
            break;
        default:
            alert("not a command");
            break;
    }
}

//create room
function createRoom(arrayCommand) {
    parent = arrayCommand[2];
    child  = arrayCommand[1];

    
    CHAT_CLIENT.createRoom(child, parent);
    //add(child, parent, child + ".html");
    //a();
}

//join room
function joinRoom(arrayCommand) {
    room = arrayCommand[1];

    CHAT_CLIENT.joinRoom(room);
}

//chat to user
function chatUser(arrayCommand) {

    user    = arrayCommand[1];
    chat    = arrayCommand[2];

    //lom jelas nih
    CHAT_CLIENT.chatToUser(user, chat);
}

//add friend
function addFriend(arrayCommand){
    user    = arrayCommand[1];

    CHAT_CLIENT.addFriend(user);
}

//exit room
function exit() {
    CHAT_CLIENT.exit();
}

//info user
function info(arrayCommand) {
    
}

//kick user
function kick(arrayCommand) {
    user    = arrayCommand[1];

    CHAT_CLIENT.kickUser(user);
}

//ban user
function ban(arrayCommand) {
    user    = arrayCommand[1];

    CHAT_CLIENT.banUser(user);
}

//unban user
function unban(arrayCommand) {
    user    = arrayCommand[1];

    CHAT_CLIENT.unbanUser(user);
}

//persist room
function persist(arrayCommand) {
    room    = arrayCommand[1];

    CHAT_CLIENT.setRoomPersistance(room);
}

//set owner
function setOwner(arrayCommand) {
    room    = arrayCommand[1];
    user    = arrayCommand[2];

    CHAT_CLIENT.setRoomOwner(room, user);
}

window.onload = function() {
    //set focus ke input
    $('chattext').focus();
}